package com.bishe.controller;


import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bishe.entity.Comment;
import com.bishe.entity.Order;
import com.bishe.entity.User;
import com.bishe.entity.response.R;
import com.bishe.service.CommentService;
import com.bishe.service.OrderService;
import com.bishe.service.UserService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * (Comment)表控制层
 *
 * @author makejava
 * @since 2024-03-07 14:47:04
 */
@Api(tags = "评论")
@RestController
@RequestMapping("comment")
public class CommentController {
    /**
     * 服务对象
     */
    @Resource
    private CommentService commentService;

    @Resource
    private OrderService orderService;

    @Resource
    private UserService userService;

    /**
     * 分页查询所有数据
     *
     * @param page    分页对象
     * @param comment 查询实体
     * @return 所有数据
     */
    @GetMapping("page")
    public R selectAll(Page<Comment> page, Comment comment) {

        StpUtil.checkLogin();
        List<Comment> list = commentService.list();

            List<Order> orderList = orderService.list(Wrappers.lambdaQuery(Order.class)
                .eq(Order::getUid, StpUtil.getLoginIdAsString())
                .eq(Order::getStatus, 2)
        );

        List<Comment> arrayList = new ArrayList<>();
        for (Comment comment1 : list) {
            for (Order order : orderList) {
                if (order.getId().equals(comment1.getUid())){
                    String uid = order.getUid();
                    if (StrUtil.isNotEmpty(uid)){
                        User user = userService.getById(uid);
                        comment1.setName(user.getName());
                        arrayList.add(comment1);
                    }
                }
            }
        }

        Page<Comment> result = new Page<>();
        result.setCurrent(page.getCurrent());
        result.setSize(page.getSize());
        result.setTotal(arrayList.size());
        result.setRecords(arrayList);
        return R.ok(result);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return R.ok(this.commentService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param comment 实体对象
     * @return 新增结果
     */
    @PostMapping
    public R insert(@RequestBody Comment comment) {

        String uid = comment.getUid();
        Order one = orderService.getOne(Wrappers.lambdaQuery(Order.class).eq(Order::getId, uid));
        if (one != null){
            one.setIsComment(1);
            orderService.updateById(one);
        }

        return R.ok(this.commentService.save(comment));
    }

    /**
     * 修改数据
     *
     * @param comment 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R update(@RequestBody Comment comment) {
        return R.ok(this.commentService.updateById(comment));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestParam("idList") List<Long> idList) {
        return R.ok(this.commentService.removeByIds(idList));
    }
}

